import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class AuthService extends BaseService<any> {
  private readonly logger = new Logger(AuthService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * checkSiteAuth
   * 对应 PHP: AuthService_admin::checkSiteAuth()
   * 逻辑类型: undefined - undefined
   */
  async checkSiteAuth(request: any) {
    // 基于PHP真实逻辑: checkSiteAuth
    // PHP原文: $site_id = $request->adminSiteId();         //todo  将站点编号转化为站点id         $site_info = ( new CoreSiteService() )->getSiteCache($site_id);         //站点不...
const site_id = request.adminSiteId(];
        //todo  将站点编号转化为站点id
        const site_info = ( this.coreSiteService ).getSiteCache(site_id);
        //站点不存在
        if (!site_info)) throw new AuthBusinessException('SITE_NOT_EXIST');
        //没有当前站点的信息
        if (!AuthService.isSuperAdmin()) {
            if (!this.getAuthRole(site_id)) throw new AuthBusinessException('NO_SITE_PERMISSION');
  }

  /**
   * checkRole
   * 对应 PHP: AuthService_admin::checkRole()
   * 逻辑类型: undefined - undefined
   */
  async checkRole(request: any) {
    // 基于PHP真实逻辑: checkRole
    // PHP原文: $rule = strtolower(trim($request->rule()->getRule()));         $method = strtolower(trim($request->method()));         $site_info = ( new AuthSiteServ...
const rule = strtolower(trim(request.rule().getRule())];
        const method = strtolower(trim(request.method())];
        const site_info = ( this.authSiteService ).getSiteInfo(];
        if (method != 'get') {
            if (site_info[ 'status' ) == SiteDict.EXPIRE) throw new AuthBusinessException('SITE_EXPIRE_NOT_ALLOW');
            if (site_info[ 'status' ) == SiteDict.CLOSE) throw new AuthBusinessException('SITE_CLOSE_NOT_ALLOW');
  }

  /**
   * getAuthRole
   * 对应 PHP: AuthService_admin::getAuthRole()
   * 逻辑类型: undefined - undefined
   */
  async getAuthRole(site_id: number) {
    // 基于PHP真实逻辑: getAuthRole
    // PHP原文: $user_role_service = new UserRoleService();         return $user_role_service->getUserRole($site_id, $this->uid);     }      /**      * 当前授权用户接口权限    ...
const user_role_service = this.userRoleService;
        return user_role_service.getUserRole(site_id, this.uid);
    }

    /**
     * 当前授权用户接口权限
     * @return */
    async getAuthApiList()
    {
        if (AuthService.isSuperAdmin()) {
            const is_admin = 1;
  }

  /**
   * getAuthApiList
   * 对应 PHP: AuthService_admin::getAuthApiList()
   * 逻辑类型: undefined - undefined
   */
  async getAuthApiList() {
    // 基于PHP真实逻辑: getAuthApiList
    // PHP原文: if (AuthService::isSuperAdmin()) {             $is_admin = 1;...
if (AuthService.isSuperAdmin()) {
            const is_admin = 1;
  }

  /**
   * getAuthMenuList
   * 对应 PHP: AuthService_admin::getAuthMenuList()
   * 逻辑类型: undefined - undefined
   */
  async getAuthMenuList(is_tree: number, addon: number) {
    // 基于PHP真实逻辑: getAuthMenuList
    // PHP原文: if (AuthService::isSuperAdmin()) {             $is_admin = 1;...
if (AuthService.isSuperAdmin()) {
            const is_admin = 1;
  }

  /**
   * getAuthInfo
   * 对应 PHP: AuthService_admin::getAuthInfo()
   * 逻辑类型: undefined - undefined
   */
  async getAuthInfo() {
    // 基于PHP真实逻辑: getAuthInfo
    // PHP原文: return ( new SiteUserService() )->getInfo($this->uid);     }      /**      * 修改用户权限      * @param string $field      * @param $data      * @return boo...
return ( this.siteUserService ).getInfo(this.uid);
    }

    /**
     * 修改用户权限
     * @param string field
     * @param data
     * @return bool
     */
    async modifyAuth(string field, data)
    {
        return ( this.siteUserService ).modify(this.uid, field, data);
    }

    /**
     * 修改用户
     * @param data
     * @return true
     */
    async editAuth(data)
    {
        if (!!data[ 'password' ))) {
            //检测原始密码是否正确
            const user = ( this.userService ).find(this.uid);
            if (!check_password(data[ 'original_password' ), user.password))
                throw new AuthBusinessException('OLD_PASSWORD_ERROR');
  }

  /**
   * modifyAuth
   * 对应 PHP: AuthService_admin::modifyAuth()
   * 逻辑类型: undefined - undefined
   */
  async modifyAuth(field: string, data: string) {
    // 基于PHP真实逻辑: modifyAuth
    // PHP原文: return ( new SiteUserService() )->modify($this->uid, $field, $data);     }      /**      * 修改用户      * @param array $data      * @return true      */ ...
return ( this.siteUserService ).modify(this.uid, field, data);
    }

    /**
     * 修改用户
     * @param data
     * @return true
     */
    async editAuth(data)
    {
        if (!!data[ 'password' ))) {
            //检测原始密码是否正确
            const user = ( this.userService ).find(this.uid);
            if (!check_password(data[ 'original_password' ), user.password))
                throw new AuthBusinessException('OLD_PASSWORD_ERROR');
  }

  /**
   * editAuth
   * 对应 PHP: AuthService_admin::editAuth()
   * 逻辑类型: undefined - undefined
   */
  async editAuth(data: any[]) {
    // 基于PHP真实逻辑: editAuth
    // PHP原文: if (!empty($data[ 'password' ])) {             //检测原始密码是否正确             $user = ( new UserService() )->find($this->uid);             if (!check_passwo...
if (!!data[ 'password' ))) {
            //检测原始密码是否正确
            const user = ( this.userService ).find(this.uid);
            if (!check_password(data[ 'original_password' ), user.password))
                throw new AuthBusinessException('OLD_PASSWORD_ERROR');
  }

  /**
   * isSuperAdmin
   * 对应 PHP: AuthService_admin::isSuperAdmin()
   * 逻辑类型: undefined - undefined
   */
  async isSuperAdmin() {
    // 基于PHP真实逻辑: isSuperAdmin
    // PHP原文: $super_admin_uid = Cache::get('super_admin_uid');          if (!$super_admin_uid) {             $super_admin_uid = ( new SysUserRole() )->where([     ...
const super_admin_uid = Cache.get('super_admin_uid');

        if (!super_admin_uid) {
            const super_admin_uid = ( this.sysUserRoleService ).where([
                [ 'site_id', '=', request().defaultSiteId() ],
                [ 'is_admin', '=', 1 ]
            ]).value('uid'];
            Cache.set('super_admin_uid', super_admin_uid);
  }
}
